<?php
class ModelContent extends CI_Model {
	
	/**
	 * Function __construct().
	 * used for Initialize ModelContent Model
	 * @autor Santu Saha
	 * @date
	*/
	function __construct()
    {
        parent::__construct();
    }
	
   /**
    * Function getList(&$config,&$start,&$param)
    * Function used For Listing content
    * @param $config string passing configaration datas
    * @param $start  string initializing start of listing
    * @param $param  string,passing parameters
    * @author Santu Saha
	* @date
    * purpose to fetch data for listing
    */
    
    function getList(&$config,&$start,&$param,$type='content')
	{
		// GET DATA FROM GET/POST  OR   SESSION ====================
		$Count = 0;
		$page = $this->uri->segment(3,0); // page
		$isSession = $this->uri->segment(4,0); // read data from SESSION or POST     (1 == POST , 0 = SESSION )
		
		$start = 0;
				
		$sortType 		= $param['sortType'];
		$sortField 		= $param['sortField'];
		$searchField 		= $param['searchField'];
		$searchString 		= $param['searchString'];
		$searchText 		= $param['searchText']; 
		$searchFromDate		= $param['searchFromDate']; 
		$searchToDate 		= $param['searchToDate']; 
		$searchDisplay 		= $param['searchDisplay']; 
		
		if($isSession == 0)
		{
			$sortType    	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'sortType','ASC');
			$sortField   	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'sortField','title');
			$searchField 	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchField','');
			$searchString 	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchString','');
			$searchText  	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchText','');
			$searchFromDate = $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchFromDate','');
			$searchToDate  	= $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchToDate','');
			$searchDisplay  = $this->nsession->get_param('ADMIN_CONTENT_'.$type,'searchDisplay',20);
		}
		
		//========= SET SESSION DATA FOR SEARCH / PAGE / SORT Condition etc =====================
		$sessionDataArray = array();
		$sessionDataArray['sortType'] 		= $sortType;
		$sessionDataArray['sortField'] 		= $sortField;
		$sessionDataArray['searchField'] 	= $searchField;
		$sessionDataArray['searchString'] 	= $searchString;
		$sessionDataArray['searchText'] 	= $searchText;		
		$sessionDataArray['searchFromDate'] = $searchFromDate;		
		$sessionDataArray['searchToDate'] 	= $searchToDate;
		$sessionDataArray['searchDisplay'] 	= $searchDisplay;		
		
		$this->nsession->set_userdata('ADMIN_CONTENT_'.$type, $sessionDataArray);
		//==============================================================
		
		$orderBy = " ORDER BY ".$sortField." ".$sortType;
		
		$where = " AND content_type = '".$type."'";
		
		if($searchField == "")
		{
		  $searchField='title';
		} 
		
		if($searchString <> "" )
		{   
		 	$where .= " AND ".$searchField. " like '%".$searchString."%' "; 
		}
		
		if($searchFromDate <> "" && $searchText=='custom')
		{
			$where.= " AND date_format(modified_date,'%Y-%m-%d') >= '".formate_date($searchFromDate,false,'Y-m-d')."' "; 
		}		
		if($searchToDate <> ""  && $searchText=='custom')
		{
			$where.= " AND date_format(modified_date,'%Y-%m-%d') <= '".formate_date($searchToDate,false,'Y-m-d')."' "; 
		}		
		if($searchText=='this_month'){
			$where.= " AND date_format(modified_date,'%Y-%m') = '".date("Y-m")."'  "; 
		}		
		if($searchText=='last_month'){
			$where.= " AND date_format(modified_date,'%Y-%m') =  date_format(now() - INTERVAL 1 MONTH, '%Y-%m') ";  
		}		
		if($searchText=='last_3_months'){
			$where.= " AND modified_date >= date_sub(NOW(), INTERVAL 3 MONTH)"; 
		}		
		if($searchText=='last_6_months'){
			$where.= " AND modified_date >= date_sub(NOW(), INTERVAL 6 MONTH) "; 
		}		
		if($searchText=='this_year'){
			$where.= " AND date_format(modified_date,'%Y') = '".date("Y")."'  "; 
		}
		if($searchText=='last_year'){
			$where.= "  AND date_format(modified_date,'%Y') =  date_format(now() - INTERVAL 1 YEAR, '%Y') "; 
		}	
		
		
		$sql="SELECT COUNT(id) as TotalrecordCount FROM ".CONTENTS."  WHERE 1 $where $orderBy";
		$recordSet = $this->db->query($sql);
		
		$config['total_rows'] = 0;
		$config['per_page'] = $searchDisplay;
		if ($recordSet)
		{
         	$row = $recordSet->row();
	   		$config['total_rows'] = $row->TotalrecordCount;
		}
		else
		{
			return false;
		}
		
		if($page > 0 && $page < $config['total_rows'] )
			$start = $page;
	
		$sql=" SELECT id,title,url,modified_date FROM ".CONTENTS." WHERE 1 $where $orderBy  LIMIT ".$start.",".$config['per_page'] ;
		
		$recordSet = $this->db->query($sql);
		
		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
				{
					foreach($row as $key=>$val){
						if(!in_array($key,$isEscapeArr)){
							$recordSet->fields[$key] = outputEscapeString($val);
						}
					}
					$rs[] = $recordSet->fields;
				}
        }
		else
		{
			return false;
		}
		return $rs;		
	}
	
	/**
    * Function delete($id)
    * Function used For Deleting content
    * @param $id  int content id 
    * @author Santu Saha
	* @date
    */
	
	function delete($id)
	{
		$sql = "DELETE FROM ".CONTENTS." WHERE id = ".$id."";
		$recordSet = $this->db->query($sql);
		
		if (!$recordSet )
		{
			return false;
		}
	}
	
	/**
    * Function addContent($data)
    * Function used For Add content
    * @param $data  array Post value
    * @author Santu Saha
	* @date
    */
	function addContent($data)
	{
		$sql = "INSERT INTO ".CONTENTS."  SET
				title				= '".$data['title']."',
				content	 			= '".$data['content']."',
				meta_title			= '".$data['meta_title']."',
				meta_keywords		= '".$data['meta_keywords']."',
				meta_description	= '".$data['meta_description']."',
				url		 			= '".$data['url']."',
				modified_date 		= NOW()  ";
		//echo $sql; die();
		$result = $this->db->query($sql);
		$last_insert_id = $this->db->insert_id(); 
		
		if($result)
		{
			return $last_insert_id;
		}
		else
		{
			return false;
		}
	}
	
	/**
    * Function editContent($id,$data)
    * Function used For Edit content
    * @param $id  int content id
    * @param $data  array Post value
    * @author Santu Saha
	* @date
    */
	
	function editContent($id,$data)
	{		
		$sql = "UPDATE ".CONTENTS."  SET
				title				= '".$data['title']."',
				content	 			= '".$data['content']."',
				meta_title			= '".$data['meta_title']."',
				meta_keywords		= '".$data['meta_keywords']."',
				meta_description	= '".$data['meta_description']."',
				modified_date 		= NOW() 
				WHERE id 			= ".$id."";	
	    
		$result = $this->db->query($sql);
		$affected_rows =$this->db->affected_rows();
		
		if($result)
		{
			return $affected_rows;
		}
		else
		{
			return false;
		}
	}
	
}

?>